CC := $(CROSS_COMPILE)gcc
PEER := root@192.168.31.59

CFLAGS := -Wall -Wextra
INC_DIRS += ${PWD}
INC_DIRS += ${PWD}/thirdparty


SRCS += $(shell find app -name '*.c')
SRCS += $(shell find daemon -name '*.c')
SRCS += $(shell find ota -name '*.c')
SRCS += $(shell find thirdparty -name '*.c')

OBJS := $(SRCS:.c=.o)

CFLAGS += $(addprefix -I, $(INC_DIRS))
ifdef DEBUG
CFLAGS += -ggdb
endif
ifdef SYSROOT
CFLAGS += --sysroot $(SYSROOT)
endif
CFLAGS += -D_DEFAULT_SOURCE
LDFLAGS += -lpaho-mqtt3c
LDFLAGS += -lcurl
LDFLAGS += -lcrypto

TARGET := gateway

.PHONY: all clean cross

all: $(TARGET)

$(TARGET): $(OBJS) main.o
	$(CC) $(CFLAGS) $^ -o $@ $(LDFLAGS)


test_buffer: $(OBJS) test/test_buffer.o
	-@$(CC) $(CFLAGS) $^ -o $@ $(LDFLAGS)
	-@./$@
	-@$(RM) $^ $@

app_mqtt_test: $(OBJS) test/app_mqtt_test.o
	-@$(CC) $(CFLAGS) $^ -o $@ $(LDFLAGS)
	-@./$@
	-@$(RM) $^ $@

test_message: $(OBJS) test/test_message.o
	-@$(CC) $(CFLAGS) $^ -o $@ $(LDFLAGS)
	-@./$@
	-@$(RM) $^ $@

clean:
	@$(RM) $(OBJS) $(TARGET) main.o

cross:
	@CROSS_COMPILE=toolchain/bin/arm-linux-gnueabihf- \
	SYSROOT=sysroot \
	make all
	@scp $(TARGET) $(PEER):/usr/bin/$(TARGET)

cross-debug:
	@CROSS_COMPILE=toolchain/bin/arm-linux-gnueabihf- \
	SYSROOT=sysroot \
	DEBUG=1 \
	make all
	@scp $(TARGET) $(PEER):/usr/bin/$(TARGET)

cross-autoboot: init/S99gateway
	@scp init/S99gateway $(PEER):/etc/init.d/S99gateway